package com.smartcanteen.user.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.smartcanteen.system.api.domain.SysUser;
import com.smartcanteen.user.domain.entity.Business;
import com.smartcanteen.user.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smartcanteen.user.mapper.BusinessMapper;

import com.smartcanteen.user.service.IBusinessService;

/**
 * 商家信息Service业务层处理
 *
 * @author ysj
 * @date 2025-03-13
 */
@Service
public class BusinessServiceImpl implements IBusinessService
{
    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private DeptMapper deptMapper;
    /**
     * 查询商家信息
     *
     * @param id 商家信息主键
     * @return 商家信息
     */
    @Override
    public Business selectBusinessById(Long id)
    {
        return businessMapper.selectBusinessById(id);
    }

    /**
     * 查询商家信息列表
     *
     * @param business 商家信息
     * @return 商家信息
     */
    @Override
    public List<Business> selectBusinessList(Business business)
    {
        return businessMapper.selectBusinessList(business);
    }

    /**
     * 新增商家信息
     *
     * @param sysUser 当前登录商家信息
     * @return 结果
     */
    @Override
    public int insertBusiness(SysUser sysUser)
    {
        Business business = new Business();
        business.setUserId(sysUser.getUserId());
        business.setUserName(sysUser.getUserName());
        business.setAvatar(sysUser.getAvatar());
        business.setStatus(Long.valueOf(sysUser.getStatus()));
        // 根据deptId查询部门名称
        business.setDeptName(deptMapper.getDeptName(sysUser.getDeptId()));
        // 根据deptId查询上级部门id，通过id查询上级部门名称
        business.setParentName(deptMapper.getDeptName(deptMapper.getParentId(sysUser.getDeptId())));
        business.setPhone(sysUser.getPhonenumber());
        business.setDeptId(sysUser.getDeptId());
        business.setCreatedAt(new java.util.Date());

        return businessMapper.insertBusiness(business);
    }

    /**
     * 修改商家信息
     *
     * @param business 商家信息
     * @return 结果
     */
    @Override
    public int updateBusiness(Business business)
    {
        return businessMapper.updateBusiness(business);
    }

    /**
     * 批量删除商家信息
     *
     * @param ids 需要删除的商家信息主键
     * @return 结果
     */
    @Override
    public int deleteBusinessByIds(Long[] ids)
    {
        return businessMapper.deleteBusinessByIds(ids);
    }

    /**
     * 删除商家信息信息
     *
     * @param id 商家信息主键
     * @return 结果
     */
    @Override
    public int deleteBusinessById(Long id)
    {
        return businessMapper.deleteBusinessById(id);
    }
}
